#include "SC_Timer.h"

void initWDT(uint8 clock)
{
    OPTION_REG = OPTION_REG_PSA_SEL_WDT + OPTION_REG_DIV64_WDT_512MS;       //---- 预分频比选择给WDT
}

void initTimer0(uint8 initValue)
{
    // OPTION_REG = OPTION_REG_PSA_SEL_WDT + OPTION_REG_DIV64_WDT_512MS;       //---- 预分频比选择给WDT
    T0IF = 0;
    T0IE = 0;
    TMR0 = initValue;
}

void WaitTM0(uint8 tm)
{
    TMR0 = tm;
    T0IF = 0;
    while(!T0IF);
    T0IF = 0;
}

void initTimer1(uint16 initValue)
{
    T1CON = T1CON_TMR1CS_INT + T1CON_T1CKPS_8;          // 16M/8 = 2M --> 0.5us
    TMR1H = initValue>>8;
    TMR1L = initValue&0x00FF;
}

void initTimer2(uint8 initValue)
{
    // T2CON = T2CON_CLK_SEL_INT + T2CON_T2CKPS_16 + T2CON_T2CKPS_1;        // 16M/4/16/1=250KHz  --> 4us
    T2CON = T2CON_CLK_SEL_INT + T2CON_T2CKPS_4 + T2CON_T2CKPS_1;        // 16M/4/4/1=1MHz  --> 1us
    PR2 = initValue;

    TMR2IF = 0;
    TMR2IE = 1;
    PEIE = 1;
    
    GIE = YES;
}

void WaitTM2(uint8 tm)
{
    PR2 = tm;
    TMR2ON = 1;
    while(!TMR2IF);
    TMR2IF = 0;
}